package com.hujia.hjsurprisemeal.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hujia.hjsurprisemeal.model.Restaurant;
import com.hujia.hjsurprisemeal.repository.RestaurantMapper;
import com.hujia.hjsurprisemeal.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RestaurantServiceImpl implements RestaurantService {

    @Autowired
    private RestaurantMapper restaurantMapper;

    @Override
    public int addRestaurant(Restaurant restaurant) {
        return restaurantMapper.insert(restaurant);
    }

    @Override
    public List<Restaurant> getAllRestaurants() {
        return restaurantMapper.selectList(null);
    }

    @Override
    public Restaurant getRestaurantById(int id) {
        return restaurantMapper.selectById(id);
    }

    @Override
    public int updateRestaurant(Restaurant restaurant) {
        return restaurantMapper.updateById(restaurant);
    }

    @Override
    public int deleteRestaurant(int id) {
        return restaurantMapper.deleteById(id);
    }

    // 额外的方法，使用QueryWrapper进行条件查询
    public List<Restaurant> getRestaurantsByLocation(String location) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("location", location);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 分页查询方法
    public Page<Restaurant> getRestaurantsByPage(int currentPage, int pageSize) {
        Page<Restaurant> page = new Page<>(currentPage, pageSize);
        return restaurantMapper.selectPage(page, null);
    }
}